how to use celery on centos7(flask, celery, rabbitmq, redis)¶
install python, rabbitmq and redis
yum -y install epel-release yum -y install https://centos7.iuscommunity.org/ius-release.rpm yum -y install python36u python36u-pip python36u-devel rabbitmq-server redis rabbitmq-plugins enable rabbitmq_management rabbitmq_tracing rabbitmq-server > rabbitmq.log 2>&1 & redis-server > redis.log 2>&1 &prepare code
from flask import Flask from celery import Celery import time import psutil app = Flask(__name__) app.config['CELERY_BROKER_URL'] = 'pyamqp://localhost:5672' app.config['CELERY_RESULT_BACKEND'] = 'redis://localhost:6379' celery = Celery(app.name, broker=app.config['CELERY_BROKER_URL']) celery.conf.update(app.config) @celery.task() def recv_rate(): #psutil.net_io_counters(pernic=True).keys() #while 1: old = psutil.net_io_counters(pernic=True)['eth0'].bytes_recv time.sleep(1) new = psutil.net_io_counters(pernic=True)['eth0'].bytes_recv try: return "recv_rate is " + str(round((new - old) / 1024, 1)) + " KB/s" except KeyboardInterrupt: exit() @app.route('/') def result(): result = recv_rate.delay() return str(result.get())install flask, celery and testing
pip3 install flask celery==4.4.0 ls app.py celery worker -A app.celery --loglevel=info > celery.log 2>&1 & tail -f celery.log export FLASK_APP=app.py flask run -h 0.0.0.0 > flask.log 2>&1 & curl http://127.0.0.1:5000 redis-cli -h 127.0.0.1 -p 6379 scan 0visit http://ip:15672 to get rabbitmq tracing log.
references: